<?php
/**
 * Created by PhpStorm.
 * User: Jerry
 * Date: 2017/2/15
 * Time: 19:45
 */

namespace SDXApi\Model;


use Think\Model;

/**
 * Class SeminarTemplatePageGoodsModel
 * @package SDXApi\Model
 */
class SeminarTemplatePageGoodsModel extends Model
{
	/**
	 * @var string
	 */
	protected $tableName = 'seminar_template_goods';


	/**
	 * Created by Jerry
	 * @param $page_id
	 * @param $wid
	 * @return bool|mixed
	 */
	public function findSeminarTemplateGoodsByPageId($page_id, $wid)
	{

		if (empty($wid) || empty($page_id)) {
			return false;
		}

		$where = array(
			'sg.page_id'	=>	$page_id,
			'g.is_delete'	=>	0,
			'g.is_on_sale'	=>	1,
			'g.original_img'=>	array(
				'NEQ',
				''
			),
		);

		$field = 'sg.goods_id,g.goods_name,g.goods_sn,g.goods_thumb,g.goods_img,g.goods_brief,g.is_presell,g.promote_price,g.promote_start_date,g.promote_end_date,g.goods_number,g.warn_number,g.original_img,g.is_on_sale,g.promote_goods_number,g.promote_today_number,g.is_buynow,g.spec_unit,g.spec_value,g.min_buy_num,g.delivery_type,g.delivery_cycle,gs.practical_num,sg.sort,g.is_promote';

		$data = $this
				->alias('sg')
				->field($field)
				->join('LEFT JOIN __GOODS_WID_'. $wid .'__ AS g ON sg.goods_id = g.goods_id')
				->join('LEFT JOIN __GOODS_STORAGE__ AS gs ON sg.goods_id = gs.goods_id AND gs.wid = ' . $wid)
				->where($where)
                ->group('sg.goods_id')
				->select();

		return $data;
	}

	/**
	 * 返回商品所属专题
	 * Created by Jerry
	 * @param $goods_id
	 * @return mixed
	 */
	public function findSeminarTemplateGoodsListByGoodsId($goods_id)
	{
		$time = time();
		$where = array(
			'sg.goods_id'	=>	$goods_id,
			'st.`status`'	=>	1,
			'st.start_time'	=>	array('LT', $time,),
			'st.end_time'	=>	array('GT', $time,),
			'st.image'		=>	array('NEQ', ''),
		);

		$field = 'st.id,st.`name`,st.image';

		$data = $this
					->alias('sg')
					->field($field)
					->join('LEFT JOIN __SEMINAR_TEMPLATE_PAGES__ AS sp ON sg.page_id = sp.id')
					->join('LEFT JOIN __SEMINAR_TEMPLATES__ AS st ON sp.template_id = st.id')
					->where($where)
					->group('st.id')
					->select();

		return $data;
	}
}